#include <gb/gb.h>
#include <gb/drawing.h>
#include <gb/sample.h>
#include <gb/hardware.h>
#include <stdio.h>
int i,x=0;

int joytest() {
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n");

printf("Joypad Test:\n\n");
printf("\nPress B");
waitpad(J_B);
printf("- OK!\n");
printf("\nPress A");
waitpad(J_A);
printf("- OK!\n");
printf("\nPress UP");
waitpad(J_UP);
printf("- OK!\n");
printf("\nPress DOWN");
waitpad(J_DOWN);
printf("- OK!\n");
printf("\nPress LEFT");
waitpad(J_LEFT);
printf("- OK!\n");
printf("\nPress RIGHT");
waitpad(J_RIGHT);
printf("- OK!\n");
printf("\nPress START");
waitpad(J_START);
printf("- OK!\n");
printf("\nPress SELECT");
waitpad(J_SELECT);
printf("- OK!\n\n");
printf("Successful!\n");

waitpad(J_A);
reset();
}

int drawtest() {
printf("\nDrawing Test:\n\n");
printf("You should see\n");
printf("some shapes.\n\n");
printf("Press A to begin.\n");
printf("\n\n\n\n\n\n\n\n\n\n\n");
waitpad(J_A);

circle(80, 80, 20, 1);
box(80, 120, 40, 60, 1);
box(50, 90, 10, 30, 0);
gprintf("Done. Press A.");
waitpad(J_A);
reset();
}

void cpu()
{
    printf("\nCPU Test:\n\n");
    printf("CPU Speed: ");
printf("%d", KEY1_REG);
printf("\n\n");
printf("CPU: Slowed");
cpu_slow();

printf(" - OK!\n\n");
printf("CPU: Enhanced");
cpu_fast();

printf(" - OK!\n\n");
printf("Successful!\n");
printf("\n\n\n\n\n\n\n");
waitpad(J_A);
    reset();
    }

    void credits()
{

printf("\nGB DEBUG CARTIDGE\n\n");
printf("Coded by\n");
printf("     Mauro Eldritch\n\n");
printf("www.eldritch.com.ar\n\n");
printf("ARGENTINA\n\n\n\n");
printf("A mi amigo Cris V.");
printf("\n\n\n\n\n\n");
waitpad(J_A);
    reset();
    }

void linkstat()
{
    printf("\nLink Test:\n\n");

    printf("IDLE: ");
printf("%d", IO_IDLE);
printf("\n\n");

    printf("SEND: ");
printf("%d", IO_SENDING);
printf("\n\n");

printf("RECEIVE: ");
printf("%d", IO_RECEIVING);
printf("\n\n");

printf("Error: ");
printf("%d", IO_ERROR);
printf("\n\n");
printf("\n\n\n\n\n\n\n");
waitpad(J_A);
reset();
}

void sound()
{
    printf("\nSound Test\n\n");
        printf("Press A if you can\n");
        printf("hear the sound.\n");
        printf("\n\n\n\n\n\n\n\n\n\n\n\n");
    play_sample(1, 150);

    waitpad(J_A);
    reset();
}

void main()
{
        printf("*******************\n");
        printf("*GB DEBUG CARTIDGE*\n");
        printf("*  MauroEldritch  *\n");
        printf("*    Argentina    *\n");
        printf("*******************\n\n");
        printf("A   => Joy Test\n");
        printf("B   => Draw Test\n");
        printf("UP  => CPU Test\n");
        printf("DOWN=> Link Test\n");
        printf("LEFT=> Sound Test\n");
        printf("SEL => Credits\n\n");
        while (((i & J_A) == 0) && ((i & J_B) == 0) && ((i & J_UP) == 0) && ((i & J_SELECT) == 0) && ((i & J_LEFT) == 0) && ((i & J_DOWN) == 0))
        {
        i = joypad (); //Establecemos boton del joy presionado
        if(i & J_A) { joytest(); }
        if(i & J_B) { drawtest(); }
         if(i & J_UP) { cpu(); }
         if(i & J_SELECT) { credits(); }
         if(i & J_LEFT) { sound(); }
         if(i & J_DOWN) { linkstat(); }
        }

}

